package com.iflytek.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iflytek.common.Result;
import com.iflytek.dto.NoticeDTO;
import com.iflytek.dto.PageDTO;
import com.iflytek.mapper.NoticeMapper;
import com.iflytek.model.Notice;
import com.iflytek.service.NoticeService;
import com.iflytek.utils.CopyUtil;
import com.iflytek.vo.NoticeVO;
import com.iflytek.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author h_t
 * @since 2023-09-23
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Resource
    private NoticeMapper noticeMapper;


    /*
    查询所有通知
     */
    @Override
    public Result<List<NoticeVO>> findAll(NoticeDTO dto) {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        List<Notice> noticeList = noticeMapper.selectList(queryWrapper);
        List<NoticeVO> roleVOS = CopyUtil.copyList(noticeList, NoticeVO.class);
        return Result.success(roleVOS);
    }

    @Override
    public Result<PageVO<NoticeVO>> getPage(PageDTO<NoticeDTO> dto) {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(dto.getName())) {
            queryWrapper.lambda().like(Notice::getId, dto.getName());
        }
        if (dto.getPageNum() == null) {
            dto.setPageNum(1);
        }
        if (dto.getSize() == null) {
            dto.setSize(5);
        }
        PageHelper.startPage(dto.getPageNum(), dto.getSize());
        List<Notice> noticeList = noticeMapper.selectList(queryWrapper);
        PageInfo<Notice> noticePageInfo = new PageInfo<>();
        List<NoticeVO> roleVOS = CopyUtil.copyList(noticeList, NoticeVO.class);
        PageVO<NoticeVO> pageVO = new PageVO<>();
        BeanUtils.copyProperties(dto, pageVO);
        pageVO.setTotal(noticePageInfo.getTotal());
        pageVO.setRecords(roleVOS);
        return Result.success(pageVO);
    }

    @Override
    public Result<Boolean> saveNT(NoticeDTO dto) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(dto, notice);

        int count = noticeMapper.insert(notice);
        if (count > 0) {
            return Result.success(true);
        } else {
            return Result.error("1", "新增失败");
        }
    }

    @Override
    public Result<NoticeVO> findById(Integer id) {
        Notice notice = noticeMapper.selectById(id);
        NoticeVO noticeVO = CopyUtil.copy(notice, NoticeVO.class);
        return Result.success(noticeVO);
    }

    @Override
    public Result<Boolean> DeleteNotice(Long id) {
        int notice = noticeMapper.deleteById(id);
        if (notice > 0) {
            return Result.success(true);
        }
        return Result.error("0", "删除失败");
    }

    @Override
    public Result<Boolean> updateNT(NoticeDTO dto) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(dto, notice);
        int count = noticeMapper.updateById(notice);

        if (count > 0) {
            return Result.success(true);

        } else {
            return Result.error("1", "更新失败");
        }
    }
}